﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using Utility;
using Utility.Json;
using Utility.Models;
using Hxj.Data;

namespace JYJJ.Web.Handlers
{
    /// <summary>
    /// Handlers_Order 的摘要说明
    /// </summary>
    public class Handlers_Order : IHttpHandler
    {
        JsonRetData objRetData = null;
        public void ProcessRequest(HttpContext context)
        {
            objRetData = new JsonRetData();
            string strMethon = ParamsOfRequest.Request("methon");
            if (strMethon == "getorder")
            {
                #region 获取订单捐赠列表
                string donateId = ParamsOfRequest.Request("donateId");
                string pageSize = ParamsOfRequest.Request("pageSize");
                string pageIndex = ParamsOfRequest.Request("pageIndex");

                int rowCount = 0;
                WhereClip wcp = new WhereClip();
                wcp = wcp.And(model_Donate_Order._.donateId == donateId && model_Donate_Order._.Status == 1);
                OrderByClip order = new OrderByClip(model_Donate_Order._.createDate, OrderByOperater.DESC);

                using (DataTable dt = JYJJ.BLL.ServceHelper.ServiceDonate().GetDonateOrderTable(out rowCount, wcp, order, pageIndex.ToSafeInt(), pageSize.ToSafeInt()))
                {
                    objRetData.State = "true";
                    objRetData.RetCode = rowCount.ToString();
                    objRetData.RetData = dt.ToJson();
                    context.Response.Write(JsonHelper.JsonSerializer<JsonRetData>(objRetData));
                }
                #endregion
            }
            else if (strMethon == "save_order")
            {
                SaveOrder(context);
            }
            else if (strMethon == "check_order")
            {
                string order_id = ParamsOfRequest.Request("order_id");
                model_Donate_Order order = BLL.ServceHelper.ServiceDonate().GetDonateOrderModelByOrderID(order_id);
                if (order != null)
                {
                    if (order.Status == 1)
                    {
                        context.Response.Write("1");
                    }
                    else
                    {
                        context.Response.Write("0");
                    }
                }
                else
                {
                    context.Response.Write("0");
                }
            }
        }

        private void SaveOrder(HttpContext context)
        {
            string name = ParamsOfRequest.Request("name");
            string email = ParamsOfRequest.Request("email");
            string phone = ParamsOfRequest.Request("phone");
            string Address = ParamsOfRequest.Request("Address");
            string zipCode = ParamsOfRequest.Request("zipCode");
            string donateId = ParamsOfRequest.Request("donateId");
            string rdp_xy = ParamsOfRequest.Request("rdp_xy");
            string province = ParamsOfRequest.Request("province");
            string city = ParamsOfRequest.Request("city");
            string district = ParamsOfRequest.Request("district");
            string rdo_sex = ParamsOfRequest.Request("rdo_sex");
            string niming = ParamsOfRequest.Request("niming");
            string rdo_sj = ParamsOfRequest.Request("rdo_sj");



            string PayType = ParamsOfRequest.Request("PayType");
            string amount = ParamsOfRequest.Request("amount");
            string count = ParamsOfRequest.Request("count");
            string Choice = ParamsOfRequest.Request("Choice");

            model_Donate mdl = JYJJ.BLL.ServceHelper.ServiceDonate().GetDonateModel(donateId.ToSafeInt());
            if (mdl == null)
            {
                objRetData.State = "false";
                objRetData.Message = "获取捐赠对象为空";
                objRetData.RetCode = string.Empty;
                objRetData.RetData = string.Empty;
                context.Response.Write(objRetData.AssemblyJson());
            }
            else
            {
                #region 订单实体赋值
                model_Donate_Order model = new model_Donate_Order();
                model.way = PayType.ToSafeInt();
                model.choice = Choice.ToSafeInt();

                if (model.choice == 0)
                {
                    model.amount = amount.ToSafeDecimal();
                    model.payMoney = model.amount;
                    model.num = 0;
                }
                else if (model.choice == 1)
                {
                    model.amount = 1;
                    model.num = count.ToSafeInt();
                    model.payMoney = model.amount * model.num;
                }
                else if (model.choice == 100)
                {
                    model.amount = 100;
                    model.num = count.ToSafeInt();
                    model.payMoney = model.amount * model.num;
                }
                if (niming == "1")
                {
                    model.UserId = 0;
                }
                else
                { }


                model.Address = Address;
                model.city = city;
                model.district = district;
                model.donateId = donateId.ToSafeInt();
                model.email = email;
                model.gender = rdo_sex.ToSafeInt();
                if (rdp_xy.ToSafeInt() == 1)
                {
                    model.IsAlumni = true;
                }
                else
                {
                    model.IsAlumni = false;
                }
                model.name = name;
                if (rdo_sj.ToSafeInt() == 1)
                {
                    model.needInvoice = true;
                }
                else
                {
                    model.needInvoice = false;
                }
                model.orderId = DateTime.Now.DateTimeToyyyyMMddHHmmss() + ConvertHelper.RndNum(5);
                model.phone = phone;
                model.province = province;
                model.Status = 0;
                model.zipCode = zipCode;
                #endregion
                try
                {
                    JYJJ.BLL.ServceHelper.ServiceDonate().Save(model);
                    objRetData.State = "true";
                    objRetData.Message = "创建订单成功";
                    objRetData.RetCode = model.donateId.ToString();
                    objRetData.RetData = model.orderId;
                    context.Response.Write(objRetData.AssemblyJson());
                }
                catch (Exception ex)
                {
                    Utility.Log.LogHelper.WriteError("创建订单失败", ex);
                    objRetData.State = "false";
                    objRetData.Message = "创建订单失败";
                    objRetData.RetCode = string.Empty;
                    objRetData.RetData = string.Empty;
                    context.Response.Write(objRetData.AssemblyJson());
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}